package gui.product;

import java.util.List;

import common.Result;
import common.Size;
import model.entity.Product;
import model.entity.container.ProductContainer;
import model.management.ProductManager;
import gui.common.*;

/**
 * Controller class for the edit product view.
 */
public class EditProductController extends Controller implements IEditProductController
{

  private Product before;
  private Product after;

  /**
   * Constructor.
   * 
   * @param view
   *          Reference to the edit product view
   * @param target
   *          Product being edited
   */
  public EditProductController(IView view, ProductData target)
  {
    super(view);

    this.before = target.getTag();

    construct();
  }

  //
  // Controller overrides
  //

  /**
   * Returns a reference to the view for this controller.
   * 
   * {@pre None}
   * 
   * {@post Returns a reference to the view for this controller.}
   */
  @Override
  protected IEditProductView getView()
  {
    return (IEditProductView) super.getView();
  }

  /**
   * Sets the enable/disable state of all components in the controller's view. A component should
   * be enabled only if the user is currently allowed to interact with that component.
   * 
   * {@pre None}
   * 
   * {@post The enable/disable state of all components in the controller's view have been set
   * appropriately.}
   */
  @Override
  protected void enableComponents()
  {
    getView().enableBarcode(false);

    if (getView().getSizeUnit() == SizeUnits.Count)
    {
      getView().setSizeValue("1");
      getView().enableSizeValue(false);
    }
    else
    {
      getView().enableSizeValue(true);
    }
    buildProduct();
    if (after == null)
    {
      getView().enableOK(false);
      return;
    }

    Result result = ProductManager.validateUpdateProduct(before, after);
    getView().enableOK(result.getStatus());
  }

  /**
   * Loads data into the controller's view.
   * 
   * {@pre None}
   * 
   * {@post The controller has loaded data into its view}
   */
  @Override
  protected void loadValues()
  {
    getView().setBarcode(before.getBarcode().getCode());
    getView().setDescription(before.getDescription());
    ;
    Size size = before.getSize();
    getView().setSizeValue(String.valueOf(size.getAmount()));
    getView().setSizeUnit(size.getUnit());
    getView().setShelfLife(String.valueOf(before.getShelfLife()));
    getView().setSupply(String.valueOf(before.getThreeMonthSupply()));
  }

  //
  // IEditProductController overrides
  //

  /**
   * This method is called when any of the fields in the edit product view is changed by the user.
   */
  @Override
  public void valuesChanged()
  {
    enableComponents();
  }

  /**
   * This method is called when the user clicks the "OK" button in the edit product view.
   */
  @Override
  public void editProduct()
  {
	  ProductData data = getNewProductData();
	  Result result = ProductManager.validateEditProduct(before, data);
	  
	  if(result.getStatus())
		  ProductManager.save();
		  ProductManager.editProduct(before, data);
  }

  private ProductData getNewProductData()
  {
    String description = getView().getDescription();
    String sizeValue = getView().getSizeValue();
    String shelfLife = getView().getShelfLife();
    String supply = getView().getSupply();

    ProductData data = new ProductData();
    data.setDescription(description);
    data.setSize(sizeValue);
    data.setShelfLife(shelfLife);
    data.setSupply(supply);

    return data;
  }

  private void buildProduct()
  {
    String description = getView().getDescription();
    String sizeStr = getView().getSizeValue();
    float sizeValue;
    if (sizeStr.isEmpty())
      sizeValue = 0;
    else
      sizeValue = Float.parseFloat(sizeStr);

    SizeUnits unit = getView().getSizeUnit();
    Size size = new Size(sizeValue, unit);

    String shelfLifeStr = getView().getShelfLife();
    int shelfLife;
    if (shelfLifeStr.isEmpty())
      shelfLife = 0;
    else
      shelfLife = Integer.parseInt(shelfLifeStr);

    String supplyStr = getView().getSupply();
    int supply;
    if (shelfLifeStr.isEmpty())
      supply = 0;
    else
      supply = Integer.parseInt(supplyStr);

    after = new Product(before.getBarcode(), description, size, shelfLife, supply);
    after.setContainers((List<ProductContainer>) before.getContainers());
    Result result = after.validateProduct();
    if (!result.getStatus())
    {
      after = null;
    }
  }

}
